

package com.maoshi.shop.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maoshi.shop.bean.app.param.UserRegisterParam;
import com.maoshi.shop.bean.model.User;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.common.util.PageParam;
import com.maoshi.shop.common.util.RedisUtil;
import com.maoshi.shop.dao.UserExtensionMapper;
import com.maoshi.shop.dao.UserMapper;
import com.maoshi.shop.service.UserExtensionService;
import com.maoshi.shop.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;
import java.util.Date;
import java.util.Objects;
import java.util.UUID;

/**
 * @author lgh on 2018/09/11.
 */
@Service
@AllArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private final UserMapper userMapper;
    private final UserExtensionService userExtensionService;


    @Override
    public User getUserByUserId(String userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public void setUserLevelBylevelId(Integer level) {
        userMapper.setUserLevelBylevelId(level);
        userExtensionService.setUserLevelBylevelId(level);
    }

    @Override
    public void setUserLevelByGrowth(Integer level, Integer minNeedGrowth, Integer maxNeedGrowth) {
        userMapper.setMaxUserLevelBylevelId(level,minNeedGrowth ,maxNeedGrowth);
        userExtensionService.setMaxUserLevelByLevelId(level,minNeedGrowth ,maxNeedGrowth);
    }


    /**
     * 看看有没有校验验证码成功的标识
     * @param userRegisterParam
     * @param checkRegisterSmsFlag
     */
    @Override
    public void validate(UserRegisterParam userRegisterParam, String checkRegisterSmsFlag) {
        if (StrUtil.isBlank(userRegisterParam.getCheckRegisterSmsFlag())) {
            throw new MaoshiShopBindException("验证码已过期，请重新发送验证码校验");
        } else {
            String checkRegisterSmsFlagMobile = RedisUtil.get(checkRegisterSmsFlag);
            if (!Objects.equals(checkRegisterSmsFlagMobile, userRegisterParam.getMobile())) {
                throw new MaoshiShopBindException("验证码已过期，请重新发送验证码校验");
            }
        }
    }

    @Override
    public IPage<User> getUserPage(PageParam<User> page, User user) {
        return  userMapper.getUserPage(page,user);
    }

    @Override
    public void insert(User user) {
        userMapper.insert(user);
    }

    @Override
    public void updateStatusByUserId(String userId, Integer status) {
        userMapper.updateStatusByUserId(userId,status);
    }

    @Override
    public User getByUserId(String userId) {
        return userMapper.getUserByUserId(userId);
    }
}
